package com.byCreation.Profile;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.byCreatetion.adabter.HistoryAdapter;
import com.byCreation.Control.Control;
import com.byCreation.Control.SliderMenuLeft;
import com.byCreation.t_globe.ContentActivity;
import com.byCreation.t_globe.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;


import Myclass.ConnectInternet;
import Myclass.Mytime;
import Parser.HttpGetJson;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MyHistoryActivity extends Activity implements OnClickListener,OnItemClickListener{

	// ==================ListView======================
		private ListView lstView;
		private HistoryAdapter adapter;
		public static ArrayList<HashMap<String, Object>> MyArrList;
		private HashMap<String, Object> map = new HashMap<String, Object>();
		private ProgressDialog mProgressDialog;

		
		private String[] id_detail={"You are open this page 19 times. Visit Date : 8:43 PM on 6 Feb 2014", "You are open this page 20 times. Visit Date : 11:43 PM on 29 Jan 2014", "You are open this page 8 times. Visit Date : 5:43 AM on 27 Jan 2014","You are open this page 3 times. Visit Date : 4:22 PM on 20 Jan 2014" ,"You are open this page 3 times. Visit Date : 4:22 PM on 20 Jan 2014" ,"You are open this page 64 times. Visit Date : 10:22 PM on 13 Jan 2014" ,"You are open this page 33 times. Visit Date : 3:22 AM on 5 Jan 2014" ,"You are open this page 6 times. Visit Date : 6:22 AM on 1 Jan 2014" };

		private String[] id_title={"Khao Lak -- 25 Years of Friendship","Transfer from Phuket Airport to Khao Lak", "Khao Lak Center in Khao Lak, Phang Nga","Health Services in Ban Bang Niang" ,"A History of Khao Lak -- 25 Years of Friendship" ,"Dive Schools and Dive Shops in Khao Lak Center" ,"Health Services and Massage in Khao Lak Center" ,"Money Services in Khao Lak Center" };
		
	
		// ===============GoogleAnalytics==============
		private GoogleAnalytics mGaInstance;
		private Tracker mGaTracker;
		// =============================================
		// ===================================googleAnalytics=============================
	@Override
	public void onStart() {
		super.onStart();
		// The rest of your onStart() code.
		EasyTracker.getInstance().activityStart(this); // Add this method.
		mGaTracker.sendView("MyHistory Screen");

	}

	@Override
	public void onStop() {
		super.onStop();
		// The rest of your onStop() code.
		EasyTracker.getInstance().activityStop(this); // Add this method.

	}

	// ================================================================================	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_my_history);
		
		// =============================GoogleAnalytics=========================
		mGaInstance = GoogleAnalytics.getInstance(this);
		mGaTracker = mGaInstance.getTracker(Control.IDGoogleanalytics);	
		
		
		final ImageView img_close = (ImageView) findViewById(R.id.img_close);
		 img_close.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click   
            	 finish();
               
             }
         });
		 
		 
		 
		 
			SetListView();
		 
	}

	private void SetListView()
    {
		
		
		
		
    	
			lstView = (ListView)findViewById(R.id.list_view);
			this.setProgressBarIndeterminateVisibility(true);
			lstView.setOnItemClickListener(this);
			 //=====================ProgressDialog==========================
	        mProgressDialog = new ProgressDialog(this);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
	        mProgressDialog.setMessage("Loading......");                       
	        mProgressDialog.setCancelable(false);  

		
	
			MyArrList = new ArrayList<HashMap<String, Object>>();

		
	   if(new ConnectInternet(MyHistoryActivity.this).checkInternetConnection())
		new LoadData().execute();
		
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
		// TODO Auto-generated method stub
		
		SliderMenuLeft.Content_title=MyArrList.get(position).get("title").toString();
		Control.page_id=MyArrList.get(position).get("page_id").toString();
		SliderMenuLeft.Mainmenu_List_Menu_Title.add(MyArrList.get(position).get("title").toString());

		
		startActivity(new Intent(MyHistoryActivity.this,ContentActivity.class));
		
		
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	class LoadData extends AsyncTask<Object, Integer, Object> {

		
		
		boolean Cancelled=false;
		
		
		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub]
			Cancelled=true;
			super.onCancelled();
		}
		
		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			   try {
								
				   
				   JSONObject jsonObj=null;
					jsonObj = new JSONObject(HttpGetJson.GetJson(Control.getAPI_MyHistory(Control.UserName),Control.user,Control.pass));
					JSONArray jsonarray_data=jsonObj.getJSONArray("history");
									
								
					
					
						 for (int i = 0; i <jsonarray_data.length(); i++)
						 { 
							 
							 JSONObject jsonObj_data=jsonarray_data.getJSONObject(i);
							 map = new HashMap<String, Object>();
							
							 
							 map.put("title",jsonObj_data.getString("page_name"));
							 map.put("detail","You are open this page "+jsonObj_data.getString("count_open")+" time");
							 map.put("img",jsonObj_data.getString("images"));
							 map.put("visit_date","Visit Date "+Mytime.ConvertFormatN("yyyy-MM-dd k:m:s", "h:mm a  d MMM yyyy", jsonObj_data.getString("visit_date")));
							 map.put("page_id",jsonObj_data.getString("page_id"));
							 
							 
							 
						     MyArrList.add(map);
						 }
						 
				 
				 
			   } catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
				 
				 
//				 try {
//						
//						JSONObject jsonObj=null;
//						jsonObj = new JSONObject(HttpGetJson.GetJson(Control.getAPI_Discount_Coupon(),Control.user,Control.pass));
//						JSONArray jsonarray_data=jsonObj.getJSONArray("discount");
//							
//						
//						 for (int i = 0; i <jsonarray_data.length(); i++)
//						 { 
//							 
//							 map = new HashMap<String, Object>();
//							 JSONObject jsonObj_data=jsonarray_data.getJSONObject(i);
//
//							
//							 map.put("title",jsonObj_data.getString("title"));
//							 map.put("detail",jsonObj_data.getString("desc"));
//							 map.put("img",jsonObj_data.getString("image"));
//							 map.put("place",jsonObj_data.getString("place"));
//							 map.put("date_promotion",jsonObj_data.getString("date_promotion"));
//							
//							 
//						     MyArrList.add(map);
//						 }
//						
//						
//						 
//						 
//					} catch (JSONException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
				 
				 
			
				 
			return null;
		}

		@Override
		protected void onPreExecute() {

			 //=====================ProgressDialog==========================
	        mProgressDialog = new ProgressDialog(MyHistoryActivity.this);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
	        mProgressDialog.setMessage("Loading......");                       
	    	mProgressDialog.setCancelable(true);
		
			
			if(mProgressDialog!=null)
				mProgressDialog.show();
			
			
			adapter=null;
			
			if (adapter == null) {
				adapter = new HistoryAdapter(MyHistoryActivity.this, MyArrList);
				lstView.setAdapter(adapter);
				
				Log.e("TG", "adapter = new NotiListAdapter(getActivity(), MyArrList);");
				
			}

			
			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");
			
			
		}

		@Override
		protected void onPostExecute(Object result) {
		
			if(mProgressDialog!=null)
				mProgressDialog.dismiss();
			
			
			if(!Cancelled)
			{
				runOnUiThread (new Thread(new Runnable() { 
			         public void run() {
			                adapter.notifyDataSetChanged();      
			                
	
							Log.e("TG", " adapter.notifyDataSetChanged();  ");
			         }
			     }));
			}
			
		}

	}


	
	
	
	
}
